<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Page extends AdminBackEndController {
	public function __construct()
	{
		parent::__construct();
        $this->load->model('repo/page_model', 'page_model');
    }

    public function index(){
        $aPage = $this->page_model->getAllPage();
        $aData = array(
            'pageTitle'=>'Trang',
            'aPage'=>$aPage
            );
        $this->renderView('backend/admin/page/index', $aData);
    }

    public function insert(){
        $oPage = new Page_Entity();
        $oPage->setPublishFlg(1);
        $aData = array(
            'pageTitle'=>'Thêm mới | Trang',
            'oPage'=> $oPage,
            'action' => admin_page_url(PAGE_SLUG.'/them-moi/do'),
            'breadcrumbTitle' => 'Thêm trang mới'
        );
        $this->renderView('backend/admin/page/single', $aData);
    }

    public function update($slug){
        $aCol = array('id', 'title', 'slug', 'position', 'order', 'content', 'template', 'publish_flg');
        $oPage = $this->page_model->getPageBySlug($slug, $aCol, array('deleted_at'=>NULL));
        if($oPage==null){ show_404(); }
        $aData = array(
            'pageTitle'=>'Cập nhật | Trang',
            'oPage'=> $oPage,
            'action' => admin_page_url(PAGE_SLUG.'/cap-nhat/do'),
            'breadcrumbTitle' => $oPage->getTitle()
        );
        $this->renderView('backend/admin/page/single', $aData);
    }

    public function doInsert(){
        $aResult = array();
        $page = $this->input->post('page', true);
        $this->form_validation->set_rules('page[title]',    'lang:page_title',    'trim|required|max_length[100]|xss_clean');
        $this->form_validation->set_rules('page[slug]',     'lang:page_slug',     'trim|required|callback_slug_check|xss_clean');
        $this->form_validation->set_rules('page[position]', 'lang:page_position', 'trim|required|integer|max_length[10]|xss_clean');
        $this->form_validation->set_rules('page[order]',    'lang:page_order',    'trim|required|integer|max_length[10]|xss_clean');
        $this->form_validation->set_rules('page[template]', 'lang:page_template', 'trim|max_length[100]');
        $this->form_validation->set_error_delimiters('<li>', '</li>');
        if ($this->form_validation->run() == false) {
            $aResult = array(
                'code' => 0,
                'title'=> $this->lang->line('insert_fail'),
                'text' => '<ul>'.validation_errors().'</ul>'
                );
        } else {
            $iPageExist = $this->page_model->checkExistsPage(0, $page);
            if($iPageExist > 0){
                $aResult = array(
                    'code' => 0,
                    'title' => $this->lang->line('insert_fail'),
                    'text'=> '<ul><li>'.$this->lang->line('page_exists').'</li></ul>'
                    );
            }
        }
        if(count($aResult)==0){
            if(isset($page['publish_flg']) && $page['publish_flg']==1){
                $page['publish_flg'] = 1;
            } else {
                $page['publish_flg'] = 0;
            }
            $page['created_at'] = get_current_date();
            $result = $this->page_model->insert($page);
            if($result){
                $aResult = array(
                'code' => 1,
                'text'=> $this->lang->line('insert_success'),
                'href' => admin_page_url(PAGE_SLUG.'/'.$page['slug'].'/cap-nhat')
                );
            } else {
                $aResult = array(
                    'code' => 0,
                    'title' => $this->lang->line('system_error')
                    );
            }
        }
        $this->renderJSON($aResult);
    }

    public function doUpdate(){
        $aResult = array();
        $page = $this->input->post('page', true);
        $tPage = $this->input->post('page');
        if(isset($page['id']) && $page['id']!=''){
            $curPageId = $this->encrypt->decode($tPage['id']);
            if($curPageId==false){
                $aResult = array(
                    'code' => 0,
                    'title'=> $this->lang->line('system_error')
                );
            } else {
                unset($page['id']);
                if(isset($page['case']) && $page['case']==UPDATE_FLASH){
                    unset($page['case']);
                    $aResult = $this->flashUpdate($page, $curPageId);
                } else {
                    unset($page['case']);
                    $aResult = $this->fullUpdate($page, $curPageId);
                }
            }
        } else {
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('system_error')
            );
        }
        $this->renderJSON($aResult);
    }

    public function fullUpdate($page, $curPageId){
        $aResult = array();
        $date    = get_current_date();
        $this->form_validation->set_rules('page[title]',    'lang:page_title',    'trim|required|max_length[100]|xss_clean');
        $this->form_validation->set_rules('page[slug]',     'lang:page_slug',     'trim|required|slug_check|xss_clean');
        $this->form_validation->set_rules('page[position]', 'lang:page_position', 'trim|required|integer|max_length[10]|xss_clean');
        $this->form_validation->set_rules('page[order]',    'lang:page_order',    'trim|required|integer|max_length[10]|xss_clean');
        $this->form_validation->set_rules('page[template]', 'lang:page_template', 'trim|max_length[100]');
        $this->form_validation->set_error_delimiters('<li>', '</li>');
        if ($this->form_validation->run() == false) {
            $aResult = array(
                'code' => 0,
                'title'=> $this->lang->line('update_fail'),
                'text' => '<ul>'.validation_errors().'</ul>'
                );
            return $aResult;
        }

        //check page exist
        $iPageExist = $this->page_model->checkExistsPage($curPageId, $page);
        if($iPageExist > 0){
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('update_fail'),
                'text'=> '<ul><li>'.$this->lang->line('page_exists').'</li></ul>'
                );
            return $aResult;
        }

        //set flag
        if(isset($page['delete_flg']) && $page['delete_flg']==1){
            $page['deleted_at'] = $date;
        } else {
            $page['deleted_at'] = NULL;
            $page['updated_at'] = $date;
        }
        unset($page['delete_flg']);

        if(isset($page['publish_flg']) && $page['publish_flg']==1){
            $page['publish_flg'] = 1;
        } else {
            $page['publish_flg'] = 0;
        }

        $result = $this->page_model->update($curPageId, $page);
        if($result){
            $aResult = array(
                'code' => 1,
                'text'=>$this->lang->line('update_success'),
                'href' => admin_page_url(PAGE_SLUG.'/'.$page['slug'].'/cap-nhat'),
                'front_href' => page_url($page['slug'])
                );
        } else {
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('system_error')
            );
        }
        return $aResult;
    }

    public function flashUpdate($page, $curPageId){
        $aResult = array();
        $tPage   = array();
        $date    = get_current_date();
        if(isset($page['delete_flg'])){
            if($page['delete_flg']==1){
                $tPage['deleted_at'] = $date;
            } else {
                $tPage['deleted_at'] = NULL;
            }
        }
        if(isset($page['publish_flg'])){
            if($page['publish_flg']==1){
                $tPage['publish_flg'] = 1;
            } else {
                $tPage['publish_flg'] = 0;
            }
        }
        if(isset($tPage['deleted_at']) && $tPage['deleted_at']==NULL){
            $tPage['updated_at'] = $date;
        }
        $result = $this->page_model->update($curPageId, $tPage);
        if($result){
            $aResult = array(
                'code' => 1,
                'text'=> $this->lang->line('update_success')
                );
        } else {
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('system_error')
                );
        }
        return $aResult;
    }
}
?>